package server;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 数据包请求体
 * @author lijiahao
 */
public class GpcRequest implements Serializable {

	private static final long serialVersionUID = 1L;
	public static final String CONTEXT_BODY = "context_body";
	public static final String X_IP = "X_IP";
	public static final String X_PORT = "X_PORT";
	public static final String X_ENTITY_ID = "X_ENTITY_ID";
	public static final String X_ENTITY_NAME = "X_ENTITY_NAME";
	public static final String X_ENTITY_TYPE = "X_TYPE";

	/**
	 * 默认构造方法
	 */
	public GpcRequest() {

	}

	/**
	 *	请求头
	 */
	private Map<String,String> headers = new HashMap<>();

	/**
	 *  请求参数
	 */
	private Map<String,Object> attributes = new HashMap<>();

	/**
	 *  请求体
	 */
	private Map<String,Object> body = new HashMap<>();
	
	private int code;
	
	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public Map<String, String> getHeaders() {
		return headers;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public Map<String, Object> getBody() {
		return body;
	}

	public void setBody(Map<String, Object> body) {
		this.body = body;
	}
	
	public GpcRequest setHeader(String key,String value) {
		if(this.headers == null) {
			this.headers = new HashMap<>(16);
		}
		this.headers.put(key, value);
		return this;
	}
	
	public void setHeaders(Map<String,String> headers) {
		if(this.headers == null) {
			this.headers = new HashMap<>(16);
		}
		this.headers.putAll(headers);
	}
	
	public String getHeader(String key) {
		return this.headers.get(key);
	}

	public void putAttribute(String key, Object value) {
		attributes.put(key, value);
	}
	
	public int length() throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsBytes(this).length;
	}
	
	
	
	
}
